import __DB__               from '../db/index.js';
import __canvas__           from '../canvas/index.js';
import __tool__             from '../tool/index.js';
import __customEvent__      from '../customEvent/index.js';
import __keycode__          from './keycode.js';

class optClass{
    constructor(){
        
    }

    init(){
        this.pullAll();
        __keycode__.init();
    }

    setPull(res){
        let type = res.type;
        let name = res.name;
        let flip = res.flip == null?'auto':res.flip;
        let pull = res.pull == null?'auto':res.pull;

        if(!type)return;

        if(type === "all"){
            this.fn_all(__canvas__.getNodeEls(),pull,flip);
            return;
        }

        if(typeof type == "number"){
            fn_group.call(this);
        }
        function fn_group(){
            let els = __canvas__.getNodeEls();
            let nodes = __DB__.getAllNodes();
            let rootData = __DB__.get('root');
            let maxGroup = rootData.maxGroup;
            
            if(type < 0 && type > maxGroup)return;

            let optNodes = nodes.filter(item=>{
                return item.group == type
            })

            let optNodes2 = optNodes.map(item=>{
                return item.id;
            })

            let newEls = [];
            els.forEach(el=>{
                newEls.push(el)
            })
            let optEls = newEls.filter(item=>{
                let id = item.getAttribute('id');
                let sign = optNodes2.indexOf(id)
                return sign > -1;
            });            
            
            optEls.forEach(el=>{
                this.nodeOptPull(el,pull,flip);
            });

            if(name === "childrenPull"){
                let childrenIds = [];
                optNodes.forEach(item=>{
                    childrenIds = childrenIds.concat(item.childrenIds);
                })
                let childEls = childrenIds.map(id=>{
                    return document.getElementById(id);
                });
                this.fn_all(childEls,pull,flip);
            }

            // 绑定自定义事件
            __customEvent__.set({
                type:"pull",
                data:{
                    label:'group',
                    group:type,
                    pull:pull
                }
            });

            __DB__.reset();
        } 
    }

    nodeOptPull(el,pull,flip){
        let id = el.getAttribute('id');
        let the = __DB__.get(id);
        
        let left  = el.querySelector('.pull-left');
        let right = el.querySelector('.pull-right');

        if(left && (flip===true || flip ==='auto')){
            let cls = left.classList;
            if(cls.length > 1){
                let newCls = pull?'pull-plus':'pull-minus';
                left.setAttribute('class',cls[0] + ' ' + newCls);
            }
            
            if(pull === 'auto'){
                the.pull.l.plus = !the.pull.l.plus;
            }
            else{
                the.pull.l.plus = pull;
            }
            
        }

        if(right && (flip===false || flip === 'auto')){
            let cls = right.classList;
            if(cls.length > 1){
                let newCls = pull?'pull-plus':'pull-minus';
                right.setAttribute('class',cls[0] + ' ' + newCls);
            }
            if(pull === 'auto'){
                the.pull.r.plus = !the.pull.r.plus;
            }
            else{
                the.pull.r.plus = pull;
            }
        }
    }

    fn_all(els,pull,flip){
        els.forEach(el=>{
            this.nodeOptPull(el,pull,flip);
        });

        // 绑定自定义事件
        __customEvent__.set({
            type:"pull",
            data:{
                label:'all',
                pull:pull
            }
        });

        __DB__.reset();

    }

    pullNode(id,flip,name,pull){
        let el = document.getElementById(id);
        let node = __DB__.get(id);

        let el_pull = null;

        if(flip === true){
            el_pull = el.querySelector('.pull-left');
        }
        else if(flip === false){
            el_pull = el.querySelector('.pull-right');
        }
        else if(flip === 'auto'){
            el_pull = node.flip?el.querySelector('.pull-left'):el.querySelector('.pull-right');
        }
        
        if(el_pull)this.pullClick(el_pull);

        if(name === "childrenPull"){
            let childrenIds = [];
            let optNodes = node.childrenIds;
            optNodes.forEach(item=>{
                childrenIds = childrenIds.concat(item);
            });
            let childEls = childrenIds.map(id=>{
                return document.getElementById(id);
            });
            this.fn_all.call(this,childEls,pull,flip);
        }
    }

    pullClick(res){
        let el = res;
        let id = el.parentNode.parentNode.getAttribute('id');

        let is = el.classList;
        let newCls = is[1] === "pull-minus"?"pull-plus":"pull-minus";
        el.setAttribute('class',is[0] + " " + newCls);
        
        let plus = false;
        let plusFlip = null;
        let the = __DB__.get(id);
        if(the.root){
            if(is[0] === "pull-left"){
                plusFlip = true;
                the.pull.l.plus = !the.pull.l.plus;
                plus = the.pull.l.plus;
            }
            else if(is[0] === "pull-right"){
                plusFlip = false;
                the.pull.r.plus = !the.pull.r.plus;
                plus = the.pull.r.plus;
            }
        }
        else{
            plusFlip = the.flip;
            if(the.flip){
                the.pull.l.plus = !the.pull.l.plus;
                plus = the.pull.l.plus;
            }
            else{
                the.pull.r.plus = !the.pull.r.plus;
                plus = the.pull.r.plus;
            }
        }

        // 绑定自定义事件
        __customEvent__.set({
            type:"pull",
            data:{
                id,
                label:the.label,
                plus,
                plusFlip
            }
        });
        
        __DB__.reset();
    }

    pullAll(){
        let el_ls = document.querySelectorAll('.pull-left');
        if(el_ls && el_ls.length > 0){
            el_ls.forEach(el=>{
                el.onclick = e=>{
                    e.stopPropagation();
                    e.preventDefault();
                    ck.call(this,{el,e});
                }
            })
        }

        let el_rs = document.querySelectorAll('.pull-right');
        if(el_rs && el_rs.length > 0){
            el_rs.forEach(el=>{
                el.onclick = e=>{
                    e.stopPropagation();
                    e.preventDefault();
                    this.pullClick(el)
                    // ck.call(this,{el,e});
                }
            })
        }

        function ck(res) {
            let el = res.el;
            let e  = res.e;
            let id = el.parentNode.parentNode.getAttribute('id');

            let is = el.classList;
            let newCls = is[1] === "pull-minus"?"pull-plus":"pull-minus";
            el.setAttribute('class',is[0] + " " + newCls);
            
            let plus = false;
            let plusFlip = null;
            let the = __DB__.get(id);
            if(the.root){
                if(is[0] === "pull-left"){
                    plusFlip = true;
                    the.pull.l.plus = !the.pull.l.plus;
                    plus = the.pull.l.plus;
                }
                else if(is[0] === "pull-right"){
                    plusFlip = false;
                    the.pull.r.plus = !the.pull.r.plus;
                    plus = the.pull.r.plus;
                }
            }
            else{
                plusFlip = the.flip;
                if(the.flip){
                    the.pull.l.plus = !the.pull.l.plus;
                    plus = the.pull.l.plus;
                }
                else{
                    the.pull.r.plus = !the.pull.r.plus;
                    plus = the.pull.r.plus;
                }
            }

            // 绑定自定义事件
            __customEvent__.set({
                type:"pull",
                data:{
                    id,
                    label:the.label,
                    plus,
                    plusFlip
                }
            });
            
            __DB__.reset();
        }
    }
}

let opt = new optClass();
export default opt;